package pe.gob.inei.sees.components
{
	import mx.collections.ArrayCollection;
	import mx.collections.CursorBookmark;
	import mx.collections.ICollectionView;
	import mx.collections.IViewCursor;
	import mx.controls.Alert;
	import mx.controls.treeClasses.*;
	import mx.events.CollectionEvent;
	import mx.events.CollectionEventKind; 

	public class SeesTreeDataDescriptor implements ITreeDataDescriptor {

		public function getChildren(node:Object, model:Object=null):ICollectionView {
			try {
				if(node is Object) {
					if(node.hijo is ArrayCollection)
						return node.hijo;
					return new ArrayCollection(node.hijo);
				}
			} catch(e:Error) {
				trace("");
			}
			return null;
		}
		
		public function hasChildren(node:Object, model:Object=null):Boolean {
			if(node == null)
				return false;
			var hijos:ICollectionView = getChildren(node, model);
			try {
				if(hijos.length > 0)
					return true;
			} catch(e:Error) {
				trace("");
			}
			return false;
		}
		
		public function isBranch(node:Object, model:Object=null):Boolean {
			try {
				if(node is Object)
					if(node.hijo != null && node.hijo.length > 0)
						return true;
			} catch(e:Error) {
				trace("");
			}
			return false;
		}
		
		public function getData(node:Object, model:Object=null):Object {
			try {
				return node;
			} catch(e:Error) {
				trace("");
			}
			return null;
		}
		
		public function addChildAt(parent:Object, child:Object, index:int, model:Object=null):Boolean {
	        var event:CollectionEvent = new CollectionEvent(CollectionEvent.COLLECTION_CHANGE);
	        event.kind = CollectionEventKind.ADD;
	        event.items = [child];
	        event.location = index;
	        if(!parent) {
	            var iterator:IViewCursor = model.createCursor();
	            iterator.seek(CursorBookmark.FIRST, index);
	            iterator.insert(child);
	        }
	        else if(parent is Object) {
	            if(parent.hijo != null) {
	                if(parent.hijo is ArrayCollection) {
	                    parent.hijo.addItemAt(child, index);
	                    if(model){
	                        model.dispatchEvent(event);
	                        model.itemUpdated(parent);
	                    }
	                    return true;
	                }
	                else {
	                    parent.hijo.splice(index, 0, child);
	                    if(model)
	                        model.dispatchEvent(event);
	                    return true;
	                }
	            }
	        }
			return false;
		}
		
		public function removeChildAt(parent:Object, child:Object, index:int, model:Object=null):Boolean {
	        var event:CollectionEvent = new CollectionEvent(CollectionEvent.COLLECTION_CHANGE);
	        event.kind = CollectionEventKind.REMOVE;
	        event.items = [child];
	        event.location = index;
	
	        if(!parent) {
	            var iterator:IViewCursor = model.createCursor();
	            iterator.seek(CursorBookmark.FIRST, index);
	            iterator.remove();
	            if(model)
	                model.dispatchEvent(event);
	            return true;
	        }
	        else if(parent is Object) {
	            if(parent.hijo != undefined) {
	                parent.hijo.splice(index, 1);
	                if(model) 
	                    model.dispatchEvent(event);
	                return true;
	            }
	        }
			return false;
		}
	}
}